var app = getApp();

Page({
    data: {
        siteInfo: app.siteInfo,
        currentIndex: -1,
        user: {
            username: '',
            phoneNumber: '',
            address: '',
            addressDetail: '',
            emailNumber: ''
        }
    },

    onLoad: function (e) {
        var that = this;
        app.initPage();
        //获取用户地址
        wx.setNavigationBarTitle({
            title: "我的收货地址",
        });
        app.util.request({
            'url': 'user/get_user_address',
            success: function (res) {
                var data = res.data.data;
                that.setData({
                    'data': data,
                });
            }
        });
    },

    // 聚焦触发
    getFocus: function (e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            currentIndex: index
        });
    },

    // 失去焦点触发
    changeIndex: function () {
        this.setData({
            currentIndex: -1
        });
    },

    // 获取地理位置
    getLocation: function () {
        var that = this;
        wx.chooseLocation({
            success: function (obj) {
                var address = obj.address;
                if (address) {
                    console.log(address);

                    var pattern = /(.+省)?(.+市)?(.+区)?/;
                    var prefectureRet = address.match(pattern);

                    var prefectureArr = [];
                    if (prefectureRet[1]) {
                        prefectureArr.push(prefectureRet[1]);
                    }
                    prefectureArr.push(prefectureRet[2]);
                    prefectureArr.push(prefectureRet[3]);

                    var prefecture = prefectureArr.join(',', prefectureArr);
                    var replacePattern = new RegExp(prefectureRet[0], "g");
                    var simpleAddress = address.replace(replacePattern, "");

                    that.setData({
                        ['data.prefectureArr']: prefectureArr,
                        ['data.prefecture']: prefecture,
                        ['data.address']: simpleAddress
                    });
                }
            }
        });
    },

    //地区选择
    bindRegionChange: function (e) {
        this.setData({
            ['data.prefecture']: e.detail.value
        })
    },

    // 保存按钮触发
    formSubmit: function (e) {
        var that = this;
        var formData = e.detail.value;
        console.log(formData);
        if (!formData.contact) {
            wx.showToast({
                title: '收货人姓名不能为空',
                icon: 'none',
            });
            return;
        }
        if (!formData.mobile) {
            wx.showToast({
                title: '手机号不能为空',
                icon: 'none',
            });
            return;
        }
        if (!formData.address) {
            wx.showToast({
                title: '详细地址不能为空',
                icon: 'none',
            });
            return;
        }

        app.util.request({
            'url': 'user/add_address',//请求链接
            'data': formData,
            success: function (res) {
                wx.navigateBack();
            }
        });
    }


})
